package com.example.zhu95.mytimber.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.listener.AlbumClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class AlbumLinearAdapter extends RecyclerView.Adapter<AlbumLinearAdapter.ViewHolder> implements SectionIndexer {
    private List<AlbumBean> albums;
    private AlbumClickListener albumClickListener;

    public void setAlbums(List<AlbumBean> albums) {
        if (albums != this.albums) {
            this.albums.clear();
            this.albums.addAll(albums);
        }
    }

    public AlbumLinearAdapter(List<AlbumBean> albums, AlbumClickListener albumClickListener) {
        this.albums = albums;
        this.albumClickListener = albumClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item_plain, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final AlbumBean album = albums.get(position);
        holder.plainItemTv1.setText(album.getAlbum());
        holder.plainItemTv2.setText(album.getArtist());
        String albumArt = album.getAlbumArt();
        // if (!TextUtils.isEmpty(albumArt)) {
        Glide.with(holder.itemView.getContext()).load(albumArt).error(R.drawable.ic_empty_music2).centerCrop().into(holder.plainItemImg);
       /* } else {
            Glide.with(holder.itemView.getContext()).load().centerCrop().into(holder.plainItemImg);
        }*/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                albumClickListener.clickAlbum(album.getId());
            }
        });

    }

    @Override
    public int getItemCount() {
        return albums.size();
    }

    @Override
    public Object[] getSections() {
        return albums.toArray();
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return sectionIndex;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= albums.size()) {
            position = albums.size() - 1;
        }
        return position;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.plain_item_img)
        ImageView plainItemImg;
        @BindView(R.id.plain_item_tv1)
        TextView plainItemTv1;
        @BindView(R.id.plain_item_tv2)
        TextView plainItemTv2;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
